<?php

include_once 'Classes/Country.class.php';

class CountryDAO {

    // Syndesh kai epilogh bashs
    function __construct(){
        $this->connection = mysql_connect('127.0.0.1', 'root', '')
                or die('Could not connect: ' . mysql_error());
        mysql_select_db('travelbook', $this->connection)
                or die('Could not select database');
    }

    // Kleisimo syndeshs me thn bash
    function __destruct(){
        if(isset($connection))
            mysql_close($connection);
    }

    // Prosthetei mia xwra sth bash
    function insertCountry($country){
        $query = "INSERT INTO country VALUES('$country->countryid','$country->name')";
        mysql_query($query);
    }

    // Epistrefei to id ths xwras analoga me to onoma ths
    function retrieveId($name){
        $query = "SELECT countryid FROM country WHERE name='$name'";
        $result = mysql_query($query);
        if(mysql_num_rows($result) == 0){
            return '';
        }
        else{
            while($row = mysql_fetch_array($result)){
                $name = $row['countryid'];
            }
            return $name;
        }
    }
}
?>
